package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskFaceOpinionLoanConditionFBinding;
import com.micangcaihang.micang.entity.FTaskFaceOpinionLoanConditionEntity;
import com.micangcaihang.micang.mvp.task.contract.FTaskFaceOpinionLoanConditionContract;
import com.micangcaihang.micang.mvp.task.presenter.FTaskFaceOpinionLoanConditionPresenter;

import butterknife.OnTouch;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 风控 面审意见放款条件
 *     version: 1.0
 * </pre>
 */

public class FTaskFaceOpinionLoanConditionFragment extends BaseFragment<FTaskFaceOpinionLoanConditionPresenter,
        FragmentTaskFaceOpinionLoanConditionFBinding> implements FTaskFaceOpinionLoanConditionContract.View {

    private String id;
    private FTaskFaceOpinionLoanConditionEntity mRequest;
    private FTaskFaceOpinionLoanConditionEntity mConditionEntity;
    private int pickerType;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }


    public static FTaskFaceOpinionLoanConditionFragment newInstance(String id) {
        Bundle args = new Bundle();
        FTaskFaceOpinionLoanConditionFragment fragment = new FTaskFaceOpinionLoanConditionFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new FTaskFaceOpinionLoanConditionEntity();
        mConditionEntity = new FTaskFaceOpinionLoanConditionEntity();
        mRequest.setId(id);
        mConditionEntity.setId(id);
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_face_opinion_loan_condition_f;
    }


    @OnTouch({R.id.et_interview_opinion, R.id.et_loan_terms})
    boolean handleNoteFieldTouch(View v, MotionEvent event) {
        v.getParent().getParent().requestDisallowInterceptTouchEvent(true);
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                v.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }
        return false;
    }

    /**
     * 返回借款人信息
     *
     * @param entity
     */
    @Override
    public void returnFaceOpinionLoanConditionData(FTaskFaceOpinionLoanConditionEntity entity) {
        isNetSuccess = true;
        mConditionEntity = entity;
        mConditionEntity.setId(id);
        String[] excludsArray = new String[]{"choose_list", "id", "mates"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mViewBinding.etInterviewOpinion.setText(entity.getInterview_opinion());
        mViewBinding.etLoanTerms.setText(entity.getLoan_terms());
    }

    public class ClickHandler {
        public void onSave(View view) {
            saveData();
        }
    }

    /**
     * 开始保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mConditionEntity.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = true;
        mPresenter.saveFaceOpinionLoanCondition(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mConditionEntity.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置请求数据
     */
    private void setSaveData() {
        mRequest.setInterview_opinion(mViewBinding.etInterviewOpinion.getText().toString());
        mRequest.setLoan_terms(mViewBinding.etLoanTerms.getText().toString());
    }
}
